﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using org.se.fs.presenter.IView.Template;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.Task.Template;

namespace org.se.fs.presenter.View.Profile
{
    public class ClientInstitutionLists : Template.ViewLists
    {
        public ClientInstitutionLists(IFormLists view) : this(view, new Task.Context.ClientInstitution()) { }

        public ClientInstitutionLists(IFormLists view, Crud task)
        {
            View = view;
            Task = task;
        }

        protected override void InitializeSearchOption()
        {
            SearchOption = new List<SearchOption>
                                {
                                    new SearchOption("Client Name", "client_id", typeof (UInt64)),
                                    new SearchOption("Institution", "institution_id", typeof (UInt64)),
                                    new SearchOption("Department", "department", typeof (string)),
                                    new SearchOption("Who Create", "user_create_id", typeof (UInt32))
                                };
            var type = (SearchOption.Select(a => a.Option)).ToArray();
            View.FillType(type);
            View.IndexType = -1;
        }

        public override void DisplayData()
        {
            View.StatusLists = string.Format("Result(s): {0} found", Lists.Rows.Count);

            if (Lists.Rows.Count.Equals(0))
                View.Lists = new DataTable();

            SetStatusPage();

            var a =
                (Lists.AsEnumerable().OrderBy(b => b.Field<string>("institution_code")).OrderBy(
                    b => b.Field<string>("institution_name")).OrderBy(b => b.Field<string>("client_middle_name")).
                    OrderBy(b => b.Field<string>("client_first_name")).OrderBy(b => b.Field<string>("client_last_name")));
            Lists = a.CopyToDataTable();
            var d = (Lists.AsEnumerable().Select(c => new
                                                          {
                                                              Client =
                                                          string.Format("{0}, {1} {2}.",
                                                                        c.Field<string>("client_last_name"),
                                                                        c.Field<string>("client_first_name"),
                                                                        c.Field<string>("client_middle_name").Substring(
                                                                            0, 1)),
                                                              Institution =
                                                          string.Format("{0}-{1}", c.Field<string>("institution_name"),
                                                                        c.Field<string>("institution_code")),
                                                              Department = c.Field<string>("department"),
                                                              UserName = c.Field<string>("user_name"),
                                                              Active = c.Field<bool>("is_active") ? "YES" : "NO"
                                                          })).ToArray();
            View.Lists = d;
        }
    }
}
